package com.aidong.home.service.impl;

import com.aidong.home.controller.concern.Success;
import com.aidong.home.dao.HomeDao;
import com.aidong.home.entity.ActivityHome;
import com.aidong.home.entity.Home;
import com.aidong.home.entity.Video;
import com.aidong.home.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by john on 17-4-21.
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    HomeDao homeDao;

    @Override
    public Success home(int cityId) {
        Map<String, Object> map = new HashMap();
        map.put("banner", homeDao.homeBanner(cityId));
        map.put("category", homeDao.courseType(cityId));
        map.put("activity", homeDao.recommendActivity(cityId));
        return new Success(map);
    }

    @Override
    public Success courseVideo(int cityId, int code, int page) {
        List<Integer> videoSeries = homeDao.videoSeries(cityId, code);
        List<Video> videoList = new ArrayList<>();
        if (videoSeries.size() > 0) {
            if (page == 0) {
                videoList = homeDao.videoList(videoSeries, 0, 5);
            } else {
                videoList = homeDao.videoList(videoSeries, (page - 1) * 10, 10);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("video", videoList);
        return new Success(map);
    }

    @Override
    public Success relation(int cityId, int code, int currentId) {
        List<Integer> videoSeries = homeDao.videoSeries(cityId, code);
        List<Video> relations = new ArrayList<>();
        if (videoSeries.size() > 0) {
            List<Video> videoList = homeDao.videoList(videoSeries, 0, 100);
            int relationSize = 4;
            if (videoList.size() < relationSize) {
                relationSize = videoList.size();
            }
            Collections.shuffle(videoList);
            for (int i = 0; i < relationSize; i++) {
                relations.add(videoList.get(i));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("relation", relations);
        return new Success(map);
    }
}
